package reduce.fz.com.reduce.fragment;


import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.StoryReply;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseFragment;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 *
 */
public class StoryReplyFragment extends BaseListFragment<StoryReplyFragment.ReplyViewHolder, StoryReply> {
    private Long storyid;
    private LayoutInflater layoutInflater;
    public StoryReplyFragment() {

    }

    public void addReply(StoryReply reply)

    {
        baseListAdapter.addData(reply);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        storyid = getArguments().getLong("storyid");
        return view;


    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        //默认返回列表
        return null;
    }

    @Override
    public ReplyViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.story_reply_item, parent, false);
        ReplyViewHolder holder = new ReplyViewHolder(view);
        return holder;
    }

    @Override
    public ReplyViewHolder createfootViewHolder(View view) {
        return new ReplyViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<StoryReply> datas, Pagination pagination) {
        if (datas != null && !datas.isEmpty())
            pagination.setStartNum(datas.get(datas.size() - 1).replyid);
        String url = ApiUtil.getApi(context, R.array.api_story_comments, storyid);

        return url;
    }

    @Override
    protected int getPageType() {
        return Pagination.TYPE_START_NUM;
    }


    /**
     * 创建故事回复fragment
     *
     * @param storid
     * @return
     */
    public static StoryReplyFragment create(Long storid) {
        Bundle bundle = new Bundle();
        bundle.putLong("storyid", storid);
        StoryReplyFragment fragment = new StoryReplyFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 创建header
     */
    public class ReplyViewHolder extends BaseRecycleViewHolder<StoryReply> {
        private TextView mTvNickName;
        private TextView mTvReplyTime;
        private TextView mTvReplyContent;
        private RoundNetworkImageView mIvUserHeader;

        public ReplyViewHolder(View itemView) {
            super(itemView);
        }

        public void initWidget(View view) {
            mTvNickName = (TextView) view.findViewById(R.id.tv_user_nickname);
            mTvReplyTime = (TextView) view.findViewById(R.id.tv_reply_time);
            mTvReplyContent = (TextView) view.findViewById(R.id.tv_reply_content);
            mIvUserHeader = (RoundNetworkImageView) view.findViewById(R.id.iv_user_header);

        }
        @Override
        public void renderView(StoryReply data, int position) {
            mTvNickName.setText(data.user.nickname);
            mTvReplyTime.setText(data.replytime+"");
            mTvReplyContent.setText(data.desc);
            mIvUserHeader.setImageUrl(data.user.avatar, HttpUtils.getImageLoader());
        }
    }


}
